package P1线性结构.LeetCode双指针;

public class LC905 {
    /*public int[] sortArrayByParity(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            if (nums[left] % 2 == 1 && nums[right] % 2 == 0) {
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
            } else if (nums[left] % 2 == 0 && nums[right] % 2 == 1) {
                left++;
                right--;
            } else if (nums[left] % 2 == 1 && nums[right] % 2 == 1) {
                right--;
            } else {
                left++;
            }
        }
        return nums;
    }*/
    //不改变元素间的相对顺序 - 考虑 稳定排序
    public int[] sortArrayByParity(int[] nums) {
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] % 2 == 1) {
                continue;
            }
            for (int j = i; j > 0 && nums[j - 1] % 2 == 1; j--) {
                int temp = nums[j];
                nums[j] = nums[j - 1];
                nums[j - 1] = temp;
            }
        }
        return nums;
    }
}